<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Get cryptographically secure random bytes</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.rand.html">« rand</a></li>
      <li style="float: right;"><a href="function.random-int.html">random_int »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.random.html">Random 函数</a></li>
    <li>Get cryptographically secure random bytes</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.random-bytes" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">random_bytes</h1>
  <p class="verinfo">(PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">random_bytes</span> &mdash; <span class="dc-title">Get cryptographically secure random bytes</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.random-bytes-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>random_bytes</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   Generates an arbitrary length string of cryptographic random bytes that are
   suitable for cryptographic use, such as when generating salts, keys or
   initialization vectors.
  </p>
  
<p class="para">
 此函数使用的随机性来源如下：
</p>
<ul class="itemizedlist">
 <li class="listitem">
  <span class="simpara">
   在 Windows 上始终使用 <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx" class="link external">&raquo;&nbsp;<span class="function"><strong>CryptGenRandom()</strong></span></a>。在
   PHP 7.2.0 起，将始终使用
   <a href="https://docs.microsoft.com/en-us/windows/desktop/SecCNG/cng-portal" class="link external">&raquo;&nbsp;CNG-API</a> 代替。
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   在 Linux 上，如果有效，将使用
   <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html" class="link external">&raquo;&nbsp;getrandom(2)</a>
   系统调用。
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   其它平台将使用 <var class="filename">/dev/urandom</var>。
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   如果上述来源均不可用，然后会抛出 <span class="classname"><a href="class.random-randomexception.html" class="classname">Random\RandomException</a></span>。
  </span>
 </li>
</ul>

  
 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <span class="simpara">
   虽然此函数是 PHP 7.0 添加到 PHP 中，但是从 PHP 5.2 到
   PHP 5.6 都可以用 <a href="https://github.com/paragonie/random_compat" class="link external">&raquo;&nbsp;用户级实现</a>。
  </span>
 </p></blockquote>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.random-bytes-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">length</code></dt>

    <dd>

     <p class="para">
      The length of the random string that should be returned in bytes; must be <code class="literal">1</code> or greater.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.random-bytes-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   A string containing the requested number of cryptographically
   secure random bytes.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.random-bytes-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="itemizedlist">
   
 <li class="listitem">
  <span class="simpara">
   如果没有找到合适的随机性来源，将会抛出 <span class="classname"><a href="class.random-randomexception.html" class="classname">Random\RandomException</a></span>。
  </span>
 </li>

   <li class="listitem">
    <span class="simpara">
     If the value of <code class="parameter">length</code> is less than <code class="literal">1</code>,
     a <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span> will be thrown.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.random-bytes-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.2.0</td>
      <td>
       In case of a <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> failure, this function will now throw a
       <span class="classname"><a href="class.random-randomexception.html" class="classname">Random\RandomException</a></span>. Previously a plain
       <span class="classname"><a href="class.exception.html" class="classname">Exception</a></span> was thrown.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.random-bytes-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="random-bytes.example.basic">
   <p><strong>示例 #1 <span class="function"><strong>random_bytes()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$bytes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
string(10) &quot;385e33f741&quot;
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.random-bytes-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="random-randomizer.getbytes.html" class="function" rel="rdfs-seeAlso">Random\Randomizer::getBytes()</a> - Get random bytes</span></li>
   <li class="member"><span class="function"><a href="function.random-int.html" class="function" rel="rdfs-seeAlso">random_int()</a> - 生成加密安全的伪随机整数</span></li>
   <li class="member"><span class="function"><a href="function.bin2hex.html" class="function" rel="rdfs-seeAlso">bin2hex()</a> - 将二进制数据转换为十六进制表示</span></li>
  </ul>
 </div>


</div></div></div></body></html>